/*
 * Hidden Equity LLC
 * Author: Maksym Fedyay, Kyiv, mcsimf@gmail.com
 * Date: 29.02.2012
 */

package com.hiddenequity.conversion_service;

import java.util.ArrayList;

import com.hiddenequity.acts.Act_Super;
import com.hiddenequity.utils.SOAPUtils;
import com.hiddenequity.utils.UrlBuncher;

public class ConversionService {
	
	public static final String URL_CONVERSION_SERVICE = UrlBuncher.URL_BASE + "ConversionTrackerService";
	
	/*
	 * Add conversion tracker
	 */
	public static String  addConversionTracker(String conversionId, String name){
		
		String operator = "ADD";
		String id = "";
		if(conversionId != null){
			operator = "SET";
			id = "<id>" + conversionId + "</id>";
		}
		
		String SOAPBody = 
				"<soap:Body>" +
					"<mutate xmlns=" + SOAPUtils.XMLNS +
						"xmlns:xsi="+ SOAPUtils.XMLNSXSI  +">" +
					"<operations>" +
						"<operator>" + operator + "</operator>" +
						"<operand xsi:type='AdWordsConversionTracker'>" +
							id +
							"<name>" + name + "</name>" +
						"</operand>" +
					"</operations>" +
				"</mutate>" +
			"</soap:Body>";
		
		String envelope = SOAPUtils.buildEnvelope( SOAPBody);
		return envelope;
	}//addConversionTracker
	
	
	
	/*
	 * Get convarsion tracker list
	 */
	public static String getConversionTrackerList(ArrayList<String> dateRange, int status){
		String SOAPBody = 
				"<soap:Body>" +
					"<get xmlns=" + SOAPUtils.XMLNS +
						"xmlns:xsi="+ SOAPUtils.XMLNSXSI  +">" +
					"<serviceSelector>" +
						"<fields>Id</fields>" +
						"<fields>Name</fields>" +
						"<fields>Status</fields>" +
						"<fields>Category</fields>" +
						"<fields>ViewthroughLookbackWindow</fields>" +
						"<fields>ViewthroughConversionDeDupSearch</fields>" +
						"<fields>IsProductAdsChargeable</fields>" +
						"<fields>ProductAdsChargeableConversionWindow</fields>" +
						"<fields>MarkupLanguage</fields>" +
						"<fields>HttpProtocol</fields>" +
						"<fields>TextFormat</fields>" +
						"<fields>ConversionPageLanguage</fields>" +
						"<fields>BackgroundColor</fields>" +
						"<fields>UserRevenueValue</fields>" +
						"<fields>NumConversionEvents</fields>" +
						"<fields>NumConvertedClicks</fields>" +
						"<fields>ConversionValue</fields>";
		
		if(status != 0){
			String statusName = "";
			switch (status) {
				case Act_Super.STATUS_ACTIVE:
					statusName = "ENABLED";
					break;
				case Act_Super.STATUS_PAUSED:
					statusName = "HIDDEN";
					break;
				case Act_Super.STATUS_DELETED:
					statusName = "DISABLED";
					break;
			}
			
			SOAPBody = SOAPBody + 
				"<predicates>" +
					"<field>Status</field>" +
					"<operator>EQUALS</operator>" +
					"<values>" + statusName + "</values>" +
				"</predicates>";
		}	
						
		
		if(!dateRange.get(0).equals("")){
			SOAPBody = SOAPBody +	"<dateRange>" +
					"<min>" + dateRange.get(0) +"</min>" +
					"<max>" + dateRange.get(1) +"</max>" +
					"</dateRange>";
		}
		
		SOAPBody = SOAPBody + 
					"</serviceSelector>" +
				"</get>" +
			"</soap:Body>";
		
		String envelope = SOAPUtils.buildEnvelope( SOAPBody);
		return envelope;
	}//getConversionTrackerList
	
	
	/*
	 * Set conversion tracker status
	 */
	public static String setConversionTrackerStatus(String conversionId, int status){
		String statusName = "";
		
		if(status != 0){
			switch (status) {
				case Act_Super.STATUS_ACTIVE:
					statusName = "ENABLED";
					break;
				case Act_Super.STATUS_PAUSED:
					statusName = "DISABLED";
					break;
				case Act_Super.STATUS_DELETED:
					statusName = "HIDDEN";
					break;
			}
		}
		
		String SOAPBody = 
				"<soap:Body>" +
					"<mutate xmlns=" + SOAPUtils.XMLNS +
						"xmlns:xsi="+ SOAPUtils.XMLNSXSI  +">" +
					"<operations>" +
						"<operator>SET</operator>" +
						"<operand xsi:type='AdWordsConversionTracker'>" +
							"<id>" + conversionId + "</id>" +
							"<status>" + statusName + "</status>" +
						"</operand>" +
					"</operations>" +
				"</mutate>" +
			"</soap:Body>";
		
		String envelope = SOAPUtils.buildEnvelope( SOAPBody);
		return envelope;
	}//setConversionTrackerStatus


	/*
	 * Set conversion settings
	 */
	public static String conversionSetSettings(String conversionId,
			String category, String markupLanguage, String httpProtocol,
			String textFormat, String conversionPageLanguage,
			String backgroundColor, String userRevenueValue) {
		
		String SOAPBody = 
				"<soap:Body>" +
					"<mutate xmlns=" + SOAPUtils.XMLNS +
						"xmlns:xsi="+ SOAPUtils.XMLNSXSI  +">" +
					"<operations>" +
						"<operator>SET</operator>" +
						"<operand xsi:type='AdWordsConversionTracker'>" +
							"<id>" + conversionId + "</id>" +
							"<category>" + category + "</category>" +
							"<markupLanguage>" + markupLanguage + "</markupLanguage>" +
							"<httpProtocol>" + httpProtocol + "</httpProtocol>" +
							"<textFormat>" + textFormat + "</textFormat>" +
							"<conversionPageLanguage>" + conversionPageLanguage + "</conversionPageLanguage>" +
							"<backgroundColor>" + backgroundColor + "</backgroundColor>" +
							"<userRevenueValue>" + userRevenueValue + "</userRevenueValue>" +
						"</operand>" +
					"</operations>" +
				"</mutate>" +
			"</soap:Body>";
		
		String envelope = SOAPUtils.buildEnvelope( SOAPBody);
		return envelope;	
	}//conversionSetSettings
}
